package com.exit66.jukebox.data;

/**
 * @author andyb
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

import java.sql.SQLException;

public class ArtistCollection extends BVDatabaseCollection {
    
    public ArtistCollection() {
        
        _currentRecord = new Artist();
        
    }
    
    public void list() {
        
        processList("", 0, 0);
        
    }
    
    public void list(String artistNameSearch) {
    	list(artistNameSearch, 0, 0);
    }
    
    public void list(String artistNameSearch, int start, int count) {
        
        if (artistNameSearch.compareTo("@num") == 0) {
            
            //processList("WHERE \"LEFT\"(artist_name, 1) between 0 and 9");
            processList("WHERE ASCII(UPPER(\"LEFT\"(artist_name, 1))) not between 65 and 90", start, count);
            
        } else if (artistNameSearch.compareTo("@var") == 0) {
            
            processList("WHERE artist_id IN (SELECT multiple_artist_id FROM album)", start, count);
            
        } else {
            
            processList("WHERE artist_order_name LIKE " + Qts(artistNameSearch), start, count);
            
        }
        
    }
    
    private void processList(String whereClause, int start, int count) {
        String sql = "";
        if (openConnection()) {
            
        	if (count == 0) 
        		sql = "SELECT ";
        	else
        		sql = "SELECT LIMIT " + start + " " + count;
        	
            _rs = retrieveData(sql + " * FROM artist " + whereClause + " ORDER BY artist_order_name");
            try {
                
                if (_rs.next()) {
                    _eof = false;
                } else {
                    _eof = true;
                }
                
            } catch (SQLException e) {
                
                _eof = true;
                
            }
            
        }
        
    }
    
    protected void loadCurrent() {
        
        try {
            
            ((Artist)_currentRecord).setArtistID(_rs.getInt("artist_id"));
            ((Artist)_currentRecord).setArtistName(_rs.getString("artist_name"));
            ((Artist)_currentRecord).setArtistOrderName(_rs.getString("artist_order_name"));
            
        } catch (SQLException e) {};
        
    }
    
}
